//加载弹窗
//校验弹窗
//校验通过之后提交表单
//加载弹窗样式

addCssLink('http://c.lagou.com/employeeWeek/css/popup.css');
	$('body').on('click','#fbWeekForm input[type="submit"]',function(){
		var form = $('body #fbWeekForm');
		fbWeekFormVal();
		var valid = form.valid();
		if(valid){
			var linkMan = $.trim($('input[name="linkMan"]',form).val());
			var companyName = $.trim($('input[name="companyName"]',form).val());
			var linkPhone = $.trim($('input[name="linkPhone"]',form).val());
			var email = $.trim($('input[name="email"]',form).val());

			employeeWeek.utils.requester({
				url: employeeWeek.requestTargets.employeeWeekUrl,
				data: {
					name:linkMan,
					companyName:companyName,
					contact:linkPhone,
					email:email
				},
				dataType:'jsonp',
				jsonp:'jsoncallback'
			},function(response){
				
				//ajax成功之后需要做的事情    调出另外一个确定弹窗
				if(response.success){
					$.colorbox({
						//window.loginPop.templates.loginPopTpl
						html:window.employeeWeek.utils.strFormat( window.employeeWeek.templates.confirmTpl),
						title:"报名抢席位"
					});
				}else{
					$.colorbox({
						//window.loginPop.templates.loginPopTpl
						html:'<div class="popup" id="employeeCancle"><p>'+response.message+'</p><span>确定</span></div>',
						title:"提示"
					});
					//alert(response.message);
				}
			});
		}
	});
	
function fengBaoEntrance(){
	//拿到弹窗
	$.colorbox({
		//window.loginPop.templates.loginPopTpl
		html:window.employeeWeek.utils.strFormat( window.employeeWeek.templates.signUpTpl),
		title:"报名抢席位"
	});

	// 点击取消按钮关闭弹窗
	$('body').on('click','#fbWeekForm a',function(){
		$.colorbox.close();
	});

	$('body').on('click','#fbWeekConfirm span',function(){
		$.colorbox.close();
	});
	
	$('body').on('click','#employeeCancle span',function(){
		$.colorbox.close();
	});
	
}

/*** check tel***********************/ 	
		jQuery.validator.addMethod("isTel", function(value, element,param) {
			var valArray = value.split(',');
			var isTel = true;
			var pattern=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^[0-9]{3,4}\-[0-9]{7,8}\-[0-9]{3,5}$)|(^[0-9]{7,8}\-[0-9]{3,5}$)|(^\([0-9]{3,4}\)[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{7,8}\-[0-9]{3,5}$)|(^1[3,4,5,7,8]{1}[0-9]{9}$)/;
			for(var i=0;i<valArray.length;i++){
				if(!pattern.test(valArray[i])){ 
					isTel = false; 
				}
			}
			if(isTel){ 
				return true; 
			}else{ 
				return false; 
			} 
		}, "请输入正确的手机号或座机号，座机格式如010-62555255或010-6255255-分机号，多个电话用英文逗号隔开");

function fbWeekFormVal(){
	$('#fbWeekForm').validate({
	    rules: {
	    	linkMan: {
		    	required: true,
		    	rangelength: [2,16]
		   	},
		   	companyName: {
		    	required: true,
		    	rangelength: [2,20]
		   	},
	   		linkPhone: {
	    		required: true,
    	    	isTel:true,
    	    	maxlength:30
		   	},
	   		email: {
		    	required: true,
		    	email:true,
		    	maxlength: 100
		   	}
		},
		messages: {
			linkMan: {
		    	required: "请输入联系人",
		    	rangelength:"请输入真实的联系人"
		   	},
		   	companyName: {
		    	required: "请输入公司名称",
		    	rangelength: "请输入有效的公司名称"
		   	},
		   	linkPhone: {
		    	required: "请输入你的手机号码或座机号码",
		    	isTel: "请输入正确的手机号或座机号",
		    	maxlength:"请输入正确的手机号或座机号"
		   	},
		   	email: {
		    	required: "请输入常用邮箱地址",
		    	email: "请输入有效的邮箱地址",
		    	maxlength:"请输入有效的邮箱地址"
		   	}
		},
		errorPlacement:function(label, element){
	    		
    		if(element.attr("type") == "text"){
    			
    			label.insertAfter(element.parent('div'));
    		}
	    }

	});
}

function addCssLink(cssURL){
    var linkTag = $('<link rel="stylesheet" type="text/css" href="' + cssURL + '" />'); 
    $($('head')[0]).append(linkTag);
} 